
#include <odkutils.h>
#include <libfmail.h>
#include <libfmail/socketipc.h>

int knhash(char v){
	if ((v >= 65) && (v <= 90))
		return v - 65;

	if ((v >= 97) && (v <= 122))
		return v - 97;
	
	return 255;
}

class FSMailbox : public ProtocolHandler{
private:
	SearchTree<int, 26, &knhash> cmdtree;
public:
	FSMailbox(){
		cmdtree.Insert("listmail", 1);
		cmdtree.Insert("sendmail", 2);
	}
    int Handle(Socket *s){
        IPCMessage *msg;
        IPC *ipc;
        int onRun, msgid;
        char *user, *pass, buffer[255], auxbuffer[255];
        FILE *fd;
        
        printf("Got Client\n");
        printf("Creating new IPC\n");
        
        ipc = new SocketIPC(s);
        onRun = 1;
        
        while(onRun){
            printf("Waiting for Message\n");
            if (ipc->PeekMessage()){
                printf("Got Message\n");
                msg = ipc->PopMessage();
				try{
                	msgid = cmdtree.Lookup(msg->GetMessageName());
				}catch (SearchNotFound){
					msgid = -1;
				}
				switch(msgid){
				case 1:
					break;
				case 2:
					break;
				default:
					break;
				}
            }
        }
        ipc->CloseIPC();
        delete ipc;
        return 0;
    }
};

class SimpleLoad : public LoadHandler{
public:
    int Dispatch(Socket *sock, ProtocolHandler *ph){
        printf("SimpleLoad: Dispatching\n");
        if (ph)
            return ph->Handle(sock);
        return 0;
    }
};

int main(int argc, char **argv){
    BaseServer *serv;
    LoadHandler *lh;
    FSMailbox *handler;
    
    printf("Creating new FSMailbox\n");
    handler = new FSMailbox();
    
    printf("Creating new SimpleLoad\n");
    lh = new SimpleLoad();
    
    printf("Creating new BaseServer\n");
    serv = new BaseServer(handler, 14001);
    
    printf("Setting Load Handler\n");
    serv->SetLoadhandler(lh);
    
    printf("Listening\n");
    serv->Listen();
}
